package com.yupi.yupicturebackend.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yupicturebackend.exception.ErrorCode;
import com.yupi.yupicturebackend.exception.ThrowUtils;
import com.yupi.yupicturebackend.model.dto.picture.PictureCommentQueryRequest;
import com.yupi.yupicturebackend.model.dto.picture.PictureCommentRequest;
import com.yupi.yupicturebackend.model.entity.PictureComment;
import com.yupi.yupicturebackend.model.vo.PictureCommentRootVo;
import com.yupi.yupicturebackend.model.vo.PictureCommentVo;
import com.yupi.yupicturebackend.service.PictureCommentService;
import com.yupi.yupicturebackend.mapper.PictureCommentMapper;
import com.yupi.yupicturebackend.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author zhonghaoming
* @description 针对表【picture_comment(评论表)】的数据库操作Service实现
* @createDate 2025-10-19 16:24:45
*/
@Service
public class PictureCommentServiceImpl extends ServiceImpl<PictureCommentMapper, PictureComment>
    implements PictureCommentService{


    @Resource
    private UserService userService;
    @Override
    public PictureComment saveOrUpdate(PictureCommentRequest pictureCommentRequest) {
        PictureComment pictureComment = new PictureComment();
        BeanUtils.copyProperties(pictureCommentRequest,pictureComment);
        if(pictureComment.getId()== null){
            this.save(pictureComment);
        }else{
            this.updateById(pictureComment);
        }
        return pictureComment;
    }
    @Override
    public QueryWrapper<PictureComment> getQueryWrapper(PictureCommentQueryRequest pictureCommentQueryRequest) {
        QueryWrapper<PictureComment> queryWrapper = new QueryWrapper<>();
        if (pictureCommentQueryRequest == null) {
            return queryWrapper;
        }
        Long pictureId = pictureCommentQueryRequest.getPictureId();
        Long targetId = pictureCommentQueryRequest.getTargetId();
        String sortField = pictureCommentQueryRequest.getSortField();
        String sortOrder = pictureCommentQueryRequest.getSortOrder();
        queryWrapper.eq(ObjUtil.isNotEmpty(pictureId), "pictureId", pictureId);
        if(ObjUtil.isNotEmpty(targetId)){
            queryWrapper.eq("targetId", targetId);
        }else{
            queryWrapper.isNull("targetId");
        }
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), sortOrder.equals("ascend"), sortField);
        return queryWrapper;
    }


    private Page<PictureCommentVo> queryPictureCommentVo(PictureCommentQueryRequest pictureCommentQueryRequest) {
        Page<PictureCommentVo> pictureCommentPage = new Page<>(pictureCommentQueryRequest.getCurrent(), pictureCommentQueryRequest.getPageSize());
        return pictureCommentPage;
    }

    @Override
    public Page<PictureCommentRootVo> getPictureCommentRootVo(PictureCommentQueryRequest pictureCommentQueryRequest) {
        ThrowUtils.throwIf(pictureCommentQueryRequest == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(pictureCommentQueryRequest.getPictureId() == null, ErrorCode.PARAMS_ERROR);
        long current = pictureCommentQueryRequest.getCurrent();
        long size = pictureCommentQueryRequest.getPageSize();
        //1.先查直接评论在图片上的targetId为空的评论
        pictureCommentQueryRequest.setTargetId(null);
        QueryWrapper<PictureComment> queryWrapper = this.getQueryWrapper(pictureCommentQueryRequest);
        Page<PictureComment> pictureCommentPage = this.page(new Page<>(current, size), queryWrapper);

        // 将PictureComment转换为PictureCommentRootVo
        Page<PictureCommentRootVo> pictureCommentRootVoPage = new Page<>(current, size, pictureCommentPage.getTotal());
        List<PictureCommentRootVo> rootVoList = pictureCommentPage.getRecords().stream().map(pictureComment -> {
            PictureCommentRootVo rootVo = new PictureCommentRootVo();
            Long userId = pictureComment.getUserId();
            rootVo.setUser(userService.getUserVO(userService.getById(userId)));
            BeanUtils.copyProperties(pictureComment, rootVo);
            return rootVo;
        }).collect(Collectors.toList());

        pictureCommentRootVoPage.setRecords(rootVoList);
        return pictureCommentRootVoPage;
    }
}




